package com.ld.control.common.equipment.method;

import com.ld.control.business.process.model.ProcessNodeExampleModel;
import com.ld.control.business.process.model.ProcessNodeExampleRunningParamModel;
import com.ld.control.business.process.model.ProcessNodeParamExampleModel;
import com.ld.control.business.process.model.ext.ProcessNodeExampleInfoExt;
import com.ld.control.business.process.service.ProcessNodeExampleService;
import com.ld.control.business.robot.model.RobotPointModel;
import com.ld.control.business.robot.service.RobotPointService;
import com.ld.control.business.variable.model.VariableExampleModel;
import com.ld.control.business.variable.service.VariableExampleService;
import com.ld.control.common.equipment.param.PHMethodParamConstant;
import com.ld.control.common.vo.EquipmentSystemMethodVo;
import com.ld.control.sysenum.MethodParamTypeEnum;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@Component
public class PHEquipmentSystemMethod {

    private static Integer[] PH_HOLE = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

    @Autowired
    private RobotPointService robotPointService;

    @Autowired
    private VariableExampleService variableExampleService;

    @Autowired
    private ProcessNodeExampleService processNodeExampleService;

    public EquipmentSystemMethodVo PHRunMethod(Long processNodeExampleRunningModelId, Map<String,
            ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        equipmentSystemMethodVo.setEquipmentMethodName(PHMethodParamConstant.RunMethod.methodName);
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();
        // 内部方法参数获取
        ProcessNodeParamExampleModel holeNumberModel = processNodeParamExampleModeMap.get("hole_number");
        ProcessNodeParamExampleModel workingDepthModel = processNodeParamExampleModeMap.get("working_depth");
        ProcessNodeParamExampleModel outerAngleModel = processNodeParamExampleModeMap.get("outer_angle");
        ProcessNodeParamExampleModel innerAngleModel = processNodeParamExampleModeMap.get("inner_angle");
        ProcessNodeParamExampleModel washAngleModel = processNodeParamExampleModeMap.get("wash_angle");
        ProcessNodeParamExampleModel washDepthModel = processNodeParamExampleModeMap.get("wash_depth");
        ProcessNodeParamExampleModel phTimesModel = processNodeParamExampleModeMap.get("ph_times");
        ProcessNodeParamExampleModel phPrecisionModel = processNodeParamExampleModeMap.get("ph_precision");


        // 孔位支持从变量里面获取
        String value = holeNumberModel.getValue();
        if (value.startsWith("@")) {
            // 去掉@前缀方便查询
            value = value.substring(1);
            // 普通变量
            Long processNodeExampleId = holeNumberModel.getProcessNodeExampleId();
            ProcessNodeExampleInfoExt processNodeExampleModelExt = processNodeExampleService.getProcessNodeExampleModelExt(processNodeExampleId);
            Long taskExampleId = processNodeExampleModelExt.getTaskExampleModel().getId();
            VariableExampleModel variableExampleModel = variableExampleService.getByTaskExampleId(taskExampleId, value);
            value = variableExampleModel.getValue();
        }
        int holeNumber = Integer.parseInt(value);
        StringBuffer positionStr = new StringBuffer();
        for (int i = 0; i < holeNumber; i++) {
            positionStr.append(PH_HOLE[i]).append(",");
        }
        positionStr = new StringBuffer(positionStr.substring(0, positionStr.length() - 1));

        ///////////////////////////////////参数定义///////////////////////////////////////

        // 需要检查的孔1,2,3,4
        ProcessNodeExampleRunningParamModel position = new ProcessNodeExampleRunningParamModel();
        position.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        position.setSendMethodParamName(PHMethodParamConstant.RunMethod.position);
        position.setSendMethodParamValue(positionStr.toString());
        position.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel workingDepth = new ProcessNodeExampleRunningParamModel();
        workingDepth.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        workingDepth.setSendMethodParamName(PHMethodParamConstant.RunMethod.working_depth);
        workingDepth.setSendMethodParamValue(workingDepthModel.getValue());
        workingDepth.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel outerAngle = new ProcessNodeExampleRunningParamModel();
        outerAngle.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        outerAngle.setSendMethodParamName(PHMethodParamConstant.RunMethod.outer_angle);
        outerAngle.setSendMethodParamValue(outerAngleModel.getValue());
        outerAngle.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel innerAngle = new ProcessNodeExampleRunningParamModel();
        innerAngle.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        innerAngle.setSendMethodParamName(PHMethodParamConstant.RunMethod.inner_angle);
        innerAngle.setSendMethodParamValue(innerAngleModel.getValue());
        innerAngle.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel washAngle = new ProcessNodeExampleRunningParamModel();
        washAngle.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        washAngle.setSendMethodParamName(PHMethodParamConstant.RunMethod.wash_angle);
        washAngle.setSendMethodParamValue(washAngleModel.getValue());
        washAngle.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel washDepth = new ProcessNodeExampleRunningParamModel();
        washDepth.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        washDepth.setSendMethodParamName(PHMethodParamConstant.RunMethod.wash_depth);
        washDepth.setSendMethodParamValue(washDepthModel.getValue());
        washDepth.setParamType(MethodParamTypeEnum.STRING.getType());

        //数据加入到集合
        processNodeExampleRunningParamModelList.add(position);
        processNodeExampleRunningParamModelList.add(workingDepth);
        processNodeExampleRunningParamModelList.add(outerAngle);
        processNodeExampleRunningParamModelList.add(innerAngle);
        processNodeExampleRunningParamModelList.add(washAngle);
        processNodeExampleRunningParamModelList.add(washDepth);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        return equipmentSystemMethodVo;
    }

}
